package com.example.chat.controller;

import com.alibaba.fastjson.JSONObject;
import com.alipay.api.AlipayApiException;
import com.alipay.api.internal.util.AlipaySignature;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.chat.beans.*;
import com.example.chat.result.*;
import com.example.chat.service.*;
import com.example.chat.utils.AlipayConfig;
import com.example.chat.utils.CheckUtils;
import org.apache.log4j.Logger;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.*;


@RestController
@RequestMapping("admin")
public class AdminController {
    @Resource
    AdminService adminService;
    @Resource
    ConfigService configService;
    @Resource
    BackgroundService backgroundService;
    @Resource
    ReportService reportService;
    @Resource
    OrderService orderService;
    @Resource
    VideoService videoService;

    @Resource
    CheckUtils checkUtils;
    @Resource
    VideoTypeService videoTypeService;

    @Resource
    ChatService chatService;
    @Resource
    ContentService contentService;
    @Resource
    PointWithdrawalService pointWithdrawalService;

    @Resource
    BannerService bannerService;

    private static Logger logger = Logger.getLogger(AdminController.class);

    /**
     * 发送验证码
     *
     * @param tel  手机号
     * @param type 类型 1注册 2修改密码
     */
    @PostMapping("sendCode")
    public ApiResult<Base> sendCode(@RequestParam(defaultValue = "", required = true) String tel,
                                    @RequestParam(defaultValue = "0", required = true) Integer type) {
        return adminService.sendCode(tel, type);
    }

    /**
     * 普通用户注册
     *
     * @param tel      手机号（用户名）
     * @param password 密码
     * @param code     验证码
     */
    @PostMapping("register")
    public ApiResult<Base> register(@RequestParam(defaultValue = "", required = true) String tel,
                                    @RequestParam(defaultValue = "", required = true) String password,
                                    @RequestParam(defaultValue = "", required = true) String code) {
        return adminService.register(tel, password, code);
    }

    /**
     * 登录
     *
     * @param username 用户名（就是手机号）
     * @param password 密码
     * @param type     1用户 2管理员
     */
    @PostMapping("login")
    public ApiResult<LoginResult> login(@RequestParam(defaultValue = "", required = true) String username,
                                        @RequestParam(defaultValue = "", required = true) String password,
                                        @RequestParam(defaultValue = "1", required = true) Integer type,
                                        HttpServletRequest request) {
        return adminService.login(username, password, request, type);
    }

    /**
     * 退出登录
     *
     * @param userId 用户id
     * @param token  token
     */
    @PostMapping("exit")
    public ApiResult<Base> exit(@RequestParam(defaultValue = "", required = true) Integer userId,
                                @RequestParam(defaultValue = "", required = true) String token) {
        return adminService.exit(userId, token);
    }

    /**
     * 上传文件
     *
     * @param file 文件
     * @param type 1图片 2html文件
     */
    @PostMapping("upload")
    public ApiResult<String> upload(@RequestParam(defaultValue = "", required = true) int type,
                                    MultipartFile file,
                                    HttpServletRequest request) {
        return adminService.upload(file, request, type);
    }

    /**
     * 修改密码-验证手机验证码
     *
     * @param tel  手机号
     * @param code 验证码
     */
    @PostMapping("forgetPasswordCode")
    public ApiResult<Base> forgetPasswordCode(@RequestParam(defaultValue = "", required = true) String tel,
                                              @RequestParam(defaultValue = "", required = true) String code) {
        return adminService.forgetPasswordCode(tel, code);
    }

    /**
     * 修改密码-设置新密码
     *
     * @param tel      手机号
     * @param password 密码
     * @param code     验证码
     */
    @PostMapping("forgetPasswordReset")
    public ApiResult<Base> forgetPasswordReset(@RequestParam(defaultValue = "", required = true) String tel,
                                               @RequestParam(defaultValue = "", required = true) String password,
                                               @RequestParam(defaultValue = "", required = true) String code) {
        return adminService.forgetPasswordReset(tel, password, code);
    }

    /**
     * 普通用户/管理员列表
     *
     * @param userId 用户id
     * @param token  用户token
     * @param type   1用户 2管理员
     * @param tel    手机号
     * @param page   页数
     * @param limit  每页数量
     */
    @PostMapping("listAdmin")
    public ListResult<List<AdminResult>> listAdmin(@RequestParam(required = true, defaultValue = "0") Integer userId,
                                                   @RequestParam(required = true, defaultValue = "") String token,
                                                   @RequestParam(required = false, defaultValue = "") String tel,
                                                   @RequestParam(required = false, defaultValue = "0") int type,
                                                   @RequestParam(required = true, defaultValue = "1") Integer page,
                                                   @RequestParam(required = true, defaultValue = "10") Integer limit) {
        return adminService.listAdmin(userId, token, tel, type, page, limit);
    }

    /**
     * 添加管理员
     *
     * @param userId   用户id
     * @param token    用户token
     * @param username 用户名
     * @param tel      电话
     * @param email    邮箱
     * @param password 密码
     * @param icon     头像
     * @param nickName 昵称
     */
    @PostMapping("addAdmin")
    public ApiResult<Base> addAdmin(@RequestParam(required = true, defaultValue = "0") Integer userId,
                                    @RequestParam(required = true, defaultValue = "") String token,
                                    @RequestParam(required = true, defaultValue = "") String username,
                                    @RequestParam(required = true, defaultValue = "123456") String password,
                                    @RequestParam(required = true, defaultValue = "") String tel,
                                    @RequestParam(required = false, defaultValue = "") String email,
                                    @RequestParam(required = false, defaultValue = "") String icon,
                                    @RequestParam(required = true, defaultValue = "") String nickName,
                                    @RequestParam(required = false, defaultValue = "2") Integer type) {
        return adminService.addAdmin(userId, token, username, password, tel, email, icon, nickName, type);
    }


    /**
     * 编辑普通用户/管理员
     *
     * @param userId   用户id
     * @param token    用户token
     * @param ids      普通用户/管理员id 编辑自己的填userId
     * @param tel      电话
     * @param email    邮箱
     * @param icon     头像
     * @param nickName 昵称
     * @param password 密码
     */
    @PostMapping("editAdmin")
    public ApiResult<Base> editAdmin(@RequestParam(required = true, defaultValue = "0") Integer userId,
                                     @RequestParam(required = true, defaultValue = "") String token,
                                     @RequestParam(required = true, defaultValue = "0") Integer ids,
                                     @RequestParam(required = true, defaultValue = "") String nickName,
                                     @RequestParam(required = true, defaultValue = "") String tel,
                                     @RequestParam(required = false, defaultValue = "") String email,
                                     @RequestParam(required = false, defaultValue = "") String icon,
                                     @RequestParam(required = false, defaultValue = "") String password) {
        return adminService.editAdmin(userId, token, ids, nickName, tel, email, icon, password);
    }

    /**
     * 用户信息详情
     *
     * @param userId 用户id
     * @param token  用户token
     * @param ids    用户id 个人信息传userId
     */
    @PostMapping("getAdmin")
    public ApiResult<AdminResult> getAdmin(@RequestParam(required = true, defaultValue = "0") Integer userId,
                                           @RequestParam(required = true, defaultValue = "") String token,
                                           @RequestParam(required = false, defaultValue = "0") Integer ids) {
        return adminService.getAdmin(userId, token, ids);
    }

    /**
     * 删除用户/管理员
     *
     * @param userId 用户id
     * @param token  用户token
     * @param ids    用户/管理员id
     */
    @PostMapping("deleteAdmin")
    public ApiResult<Base> deleteAdmin(@RequestParam(required = true, defaultValue = "0") Integer userId,
                                       @RequestParam(required = true, defaultValue = "") String token,
                                       @RequestParam(required = true, defaultValue = "ids[]") Integer[] ids) {
        return adminService.deleteAdmin(userId, token, ids);
    }

    /**
     * 同步普通用户声网uid
     *
     * @param userId 用户id
     * @param token  用户token
     * @param ids    普通用户id
     * @param uid    声网uid
     */
    @PostMapping("editAdminUid")
    public ApiResult<Base> editAdminUid(@RequestParam(required = true, defaultValue = "0") Integer userId,
                                        @RequestParam(required = true, defaultValue = "") String token,
                                        @RequestParam(required = true, defaultValue = "0") Integer ids,
                                        @RequestParam(required = true, defaultValue = "") String uid) {
        return adminService.editAdminUid(userId, token, ids, uid);
    }

    /**
     * 获取所有用户(随机聊天用)
     *
     * @param userId 用户id
     * @param token  token
     */
    @PostMapping("allAdmin")
    public ApiResult<List<AdminResult>> allAdmin(@RequestParam(defaultValue = "0", required = true) Integer userId,
                                                 @RequestParam(defaultValue = "", required = true) String token) {
        return adminService.allAdmin(userId, token);
    }

    /**
     * 系统配置信息详情
     *
     * @param userId 用户id
     * @param token  用户token
     */
    @PostMapping("getConfig")
    public ApiResult<Config> getConfig(@RequestParam(required = true, defaultValue = "0") Integer userId,
                                       @RequestParam(required = true, defaultValue = "") String token) {
        return configService.getConfig(userId, token);
    }


    /**
     * 获取备案号
     */
    @PostMapping("getCip")
    public ApiResult<String> getCip() {
        return configService.getCip();
    }

    /**
     * 修改系统配置信息详情
     *
     * @param userId           用户id
     * @param token            用户token
     * @param aliyunId         阿里云id
     * @param aliyunKey        阿里云key
     * @param shengwangAppId   声网id
     * @param monthPay         按月支付费用
     * @param yearPay          按年支付费用
     * @param oncePay          单次支付费用
     * @param record           视频录存开关 1开 2关
     * @param longRecord       最长录制时长（小时）
     * @param monthRecordTimes 每月每日最大录制次数
     * @param yearRecordTimes  每年每日最大录制次数
     * @param maxSaveTime      录制最长保存时间（月）
     * @param aliyunCode       阿里云code
     * @param cip              cip备案号
     * @param aliyunSign       阿里云签名
     * @param pointMultiple    积分倍数
     * @param unLoginChatTime  未登录聊天时间
     */
    @PostMapping("editConfig")
    public ApiResult<Base> editConfig(@RequestParam(required = true, defaultValue = "0") Integer userId,
                                      @RequestParam(required = true, defaultValue = "") String token,
                                      @RequestParam(required = true, defaultValue = "") String aliyunId,
                                      @RequestParam(required = true, defaultValue = "") String aliyunKey,
                                      @RequestParam(required = true, defaultValue = "") String shengwangAppId,
                                      @RequestParam(required = true, defaultValue = "0") double monthPay,
                                      @RequestParam(required = true, defaultValue = "0") double yearPay,
                                      @RequestParam(required = true, defaultValue = "0") double oncePay,
                                      @RequestParam(required = true, defaultValue = "1") int record,
                                      @RequestParam(required = true, defaultValue = "1") int longRecord,
                                      @RequestParam(required = true, defaultValue = "1") int monthRecordTimes,
                                      @RequestParam(required = true, defaultValue = "") int yearRecordTimes,
                                      @RequestParam(required = true, defaultValue = "2") int maxSaveTime,
                                      @RequestParam(required = true, defaultValue = "") String aliyunCode,
                                      @RequestParam(required = true, defaultValue = "") String cip,
                                      @RequestParam(required = true, defaultValue = "") String aliyunSign,
                                      @RequestParam(required = true, defaultValue = "") Integer pointMultiple,
                                      @RequestParam(required = true, defaultValue = "") Integer unLoginChatTime
    ) {
        return configService.
                editConfig(userId, token, aliyunId, aliyunKey, shengwangAppId, monthPay, yearPay, oncePay, record, longRecord,
                monthRecordTimes, yearRecordTimes, maxSaveTime, aliyunCode, cip,aliyunSign, pointMultiple, unLoginChatTime);
    }


    /**
     * 背景图列表
     *
     * @param userId  用户id
     * @param token   用户token
     * @param type    0全部 1pc 2app
     * @param appType 0全部 1引导页 2广告页 3无
     * @param status  1开启 2关闭
     * @param page    页数
     * @param limit   每页数量
     */
    @PostMapping("listBackground")
    public ListResult<List<BackgroundResult>> listBackground(@RequestParam(required = true, defaultValue = "0") Integer userId,
                                                             @RequestParam(required = true, defaultValue = "") String token,
                                                             @RequestParam(required = false, defaultValue = "0") int appType,
                                                             @RequestParam(required = false, defaultValue = "0") int type,
                                                             @RequestParam(required = false, defaultValue = "0") int status,
                                                             @RequestParam(required = true, defaultValue = "1") Integer page,
                                                             @RequestParam(required = true, defaultValue = "10") Integer limit) {
        return backgroundService.listBackground(userId, token, appType, type, status, page, limit);
    }


    /**
     * 添加背景图
     *
     * @param userId   用户id
     * @param token    用户token
     * @param type     1pc 2app
     * @param status   1开启 2关闭
     * @param appType  1引导页 2广告页 3无
     * @param filePath 背景图路径
     * @param url      链接
     */
    @PostMapping("addBackground")
    public ApiResult<Base> addBackground(@RequestParam(required = true, defaultValue = "0") Integer userId,
                                         @RequestParam(required = true, defaultValue = "") String token,
                                         @RequestParam(required = true, defaultValue = "") String filePath,
                                         @RequestParam(required = true, defaultValue = "") String url,
                                         @RequestParam(required = true, defaultValue = "1") Integer type,
                                         @RequestParam(required = true, defaultValue = "1") Integer status,
                                         @RequestParam(required = true, defaultValue = "3") Integer appType) {
        return backgroundService.addBackground(userId, token, filePath, url, type, status, appType);
    }


    /**
     * 编辑背景图
     *
     * @param userId   用户id
     * @param token    用户token
     * @param ids      背景图id
     * @param type     1pc 2app
     * @param status   1开启 2关闭
     * @param appType  1引导页 2广告页 3无
     * @param filePath 背景图路径
     * @param url      链接
     */
    @PostMapping("editBackground")
    public ApiResult<Base> editBackground(@RequestParam(required = true, defaultValue = "0") Integer userId,
                                          @RequestParam(required = true, defaultValue = "") String token,
                                          @RequestParam(required = true, defaultValue = "1") Integer ids,
                                          @RequestParam(required = true, defaultValue = "") String filePath,
                                          @RequestParam(required = true, defaultValue = "") String url,
                                          @RequestParam(required = true, defaultValue = "1") Integer type,
                                          @RequestParam(required = true, defaultValue = "1") Integer status,
                                          @RequestParam(required = true, defaultValue = "3") Integer appType) {
        return backgroundService.editBackground(userId, token, ids, filePath, url, type, status, appType);
    }


    /**
     * 删除背景图
     *
     * @param userId 用户id
     * @param token  用户token
     * @param ids    背景图id
     */
    @PostMapping("deleteBackground")
    public ApiResult<Base> deleteBackground(@RequestParam(required = true, defaultValue = "0") Integer userId,
                                            @RequestParam(required = true, defaultValue = "") String token,
                                            @RequestParam(required = true, defaultValue = "ids[]") Integer[] ids) {
        return backgroundService.deleteBackground(userId, token, ids);
    }


    /**
     * 获取所有背景图
     *
     * @param type    0全部 1pc 2app
     * @param appType 0全部 1引导页 2广告页 3无
     * @param status  0全部 1开启 2关闭
     */
    @PostMapping("allBackground")
    public ApiResult<List<BackgroundResult>> allBackground(@RequestParam(required = true, defaultValue = "0") int appType,
                                                           @RequestParam(required = true, defaultValue = "0") int type,
                                                           @RequestParam(required = true, defaultValue = "0") int status) {
        return backgroundService.allBackground(appType, type, status);
    }

    /**
     * 举报列表(普通用户只能看到自己的）
     *
     * @param userId 用户id
     * @param token  用户token
     * @param tel    被举报人手机号
     * @param phone  举报人手机号
     * @param status 1待审核 2已审核
     * @param page   页数
     * @param limit  每页数量
     */
    @PostMapping("listReport")
    public ListResult<List<ReportResult>> listReport(@RequestParam(required = true, defaultValue = "0") Integer userId,
                                                     @RequestParam(required = true, defaultValue = "") String token,
                                                     @RequestParam(required = false, defaultValue = "") String tel,
                                                     @RequestParam(required = false, defaultValue = "") String phone,
                                                     @RequestParam(required = false, defaultValue = "0") int status,
                                                     @RequestParam(required = true, defaultValue = "1") Integer page,
                                                     @RequestParam(required = true, defaultValue = "10") Integer limit) {
        return reportService.listReport(userId, token, tel, phone, status, page, limit);
    }


    /**
     * 举报用户
     *
     * @param userId  用户id
     * @param token   用户token
     * @param adminId 举报用户id
     * @param content 举报内容
     */
    @PostMapping("addReport")
    public ApiResult<Base> addReport(@RequestParam(required = true, defaultValue = "0") Integer userId,
                                     @RequestParam(required = true, defaultValue = "") String token,
                                     @RequestParam(required = true, defaultValue = "0") Integer adminId,
                                     @RequestParam(required = true, defaultValue = "") String content) {
        return reportService.addReport(userId, token, adminId, content);
    }


    /**
     * 编辑举报内容
     *
     * @param userId  用户id
     * @param token   用户token
     * @param ids     举报id
     * @param content 举报内容
     */
    @PostMapping("editReport")
    public ApiResult<Base> editReport(@RequestParam(required = true, defaultValue = "0") Integer userId,
                                      @RequestParam(required = true, defaultValue = "") String token,
                                      @RequestParam(required = true, defaultValue = "0") Integer ids,
                                      @RequestParam(required = true, defaultValue = "") String content) {
        return reportService.editReport(userId, token, ids, content);
    }


    /**
     * 删除举报内容
     *
     * @param userId 用户id
     * @param token  用户token
     * @param ids    背景图id
     */
    @PostMapping("deleteReport")
    public ApiResult<Base> deleteReport(@RequestParam(required = true, defaultValue = "0") Integer userId,
                                        @RequestParam(required = true, defaultValue = "") String token,
                                        @RequestParam(required = true, defaultValue = "ids[]") Integer[] ids) {
        return reportService.deleteReport(userId, token, ids);
    }


    /**
     * 审核举报内容
     *
     * @param userId 用户id
     * @param token  用户token
     * @param ids    举报id
     * @param status 2已审核 3未通过审核
     */
    @PostMapping("auditReport")
    public ApiResult<Base> auditReport(@RequestParam(required = true, defaultValue = "0") Integer userId,
                                       @RequestParam(required = true, defaultValue = "") String token,
                                       @RequestParam(required = true, defaultValue = "0") Integer ids,
                                       @RequestParam(required = true, defaultValue = "2") Integer status) {
        return reportService.auditReport(userId, token, ids, status);
    }

    /**
     * 流水列表
     *
     * @param userId 用户id
     * @param token  用户token
     * @param tel    消费人手机号
     * @param isMine 0所有 1获取自己的
     * @param page   页数
     * @param limit  每页数量
     */
    @PostMapping("listOrder")
    public ListResult<List<OrderResult>> listOrder(@RequestParam(required = true, defaultValue = "0") Integer userId,
                                                   @RequestParam(required = true, defaultValue = "") String token,
                                                   @RequestParam(required = false, defaultValue = "") String tel,
                                                   @RequestParam(required = false, defaultValue = "0") Integer isMine,
                                                   @RequestParam(required = true, defaultValue = "1") Integer page,
                                                   @RequestParam(required = true, defaultValue = "10") Integer limit) {
        return orderService.listOrder(userId, token, tel, isMine, page, limit);
    }


    /**
     * 个人视频录存列表
     *
     * @param userId 用户id
     * @param token  用户token
     * @param type   1视频录制 2应急呼救
     * @param page   页数
     * @param limit  每页数量
     */
    @PostMapping("listVideo")
    public ListResult<List<VideoResult>> listVideo(@RequestParam(required = true, defaultValue = "0") Integer userId,
                                                   @RequestParam(required = true, defaultValue = "") String token,
                                                   @RequestParam(required = true, defaultValue = "1") int type,
                                                   @RequestParam(required = true, defaultValue = "1") Integer page,
                                                   @RequestParam(required = true, defaultValue = "10") Integer limit) {
        return videoService.listVideo(userId, token, type, page, limit);
    }


//    /**
//     * 视频录存
//     *
//     * @param userId      用户id
//     * @param token       用户token
//     * @param chatAdminId 录制对象用户id
//     * @param filePath    视频录存路径(阿里oss)
//     */
//    @PostMapping("addVideo")
//    public ApiResult<Base> addVideo(@RequestParam(required = true, defaultValue = "0") Integer userId,
//                                    @RequestParam(required = true, defaultValue = "") String token,
//                                    @RequestParam(required = false, defaultValue = "0") Integer chatAdminId,
//                                    @RequestParam(required = true, defaultValue = "") String filePath) {
//        return videoService.addVideo(userId, token, chatAdminId, filePath);
//    }

    /**
     * 删除视频录存
     *
     * @param userId 用户id
     * @param token  用户token
     * @param ids    视频录存id
     */
    @PostMapping("deleteVideo")
    public ApiResult<Base> deleteVideo(@RequestParam(required = true, defaultValue = "0") Integer userId,
                                       @RequestParam(required = true, defaultValue = "") String token,
                                       @RequestParam(required = true, defaultValue = "ids[]") Integer[] ids) {
        return videoService.deleteVideo(userId, token, ids);
    }

    /**
     * 普通用户开始匹配聊天
     *
     * @param userId 用户id
     * @param token  用户token
     * @param type   1视频聊天 2图文聊天
     */
    @PostMapping("mateChat")
    public ApiResult<Integer> mateChat(@RequestParam(required = true, defaultValue = "0") Integer userId,
                                       @RequestParam(required = true, defaultValue = "") String token,
                                       @RequestParam(required = true, defaultValue = "1") Integer type) {
        return chatService.mateChat(userId, token, type);
    }


    /**
     * 未登录用户开始匹配聊天
     *
     * @param uuid 随机id
     * @param type 1视频聊天 2图文聊天
     */
    @PostMapping("mateChatUnLogin")
    public ApiResult<Integer> mateChatUnLogin(@RequestParam(required = true, defaultValue = "") String uuid,
                                              @RequestParam(required = true, defaultValue = "1") Integer type,
                                              HttpServletRequest request) {
        return chatService.mateChatUnLogin(uuid, type, request);
    }


    /**
     * 心跳获取
     *
     * @param userId 登录用户id
     * @param uuid   未登录uuid
     * @param ids    匹配id
     */
    @PostMapping("getChatInfo")
    public ApiResult<MateResult> getChatInfo(@RequestParam(required = true, defaultValue = "0") Integer userId,
                                             @RequestParam(required = true, defaultValue = "") String uuid,
                                             @RequestParam(required = true, defaultValue = "0") Integer ids,
                                             HttpServletRequest request) {
        return chatService.getChatInfo(userId, uuid, ids, request);
    }


    /**
     * 普通用户开始录制
     *
     * @param userId  用户id
     * @param token   用户token
     * @param channel 频道号
     * @param ids     对方id或对方重命名名称
     */
    @PostMapping("record")
    public ApiResult<RecordResult> record(@RequestParam(required = true, defaultValue = "0") Integer userId,
                                          @RequestParam(required = true, defaultValue = "") String token,
                                          @RequestParam(required = true, defaultValue = "") String channel,
                                          @RequestParam(required = true, defaultValue = "") String ids) {
        return chatService.record(userId, token, channel, ids);
    }

    /**
     * 普通用户根据resourceId和sid查询录制状态
     *
     * @param userId     用户id
     * @param token      用户token
     * @param resourceId 通过请求获取的 resource ID。
     * @param sid        通过 start 请求获取的录制 ID。
     * @param type       1普通录制 2应急求救
     */
    @PostMapping("recordStatus")
    public ApiResult<RecordStatusResult> recordStatus(@RequestParam(required = true, defaultValue = "0") Integer userId,
                                                      @RequestParam(required = true, defaultValue = "") String token,
                                                      @RequestParam(required = true, defaultValue = "") String resourceId,
                                                      @RequestParam(required = true, defaultValue = "") String sid,
                                                      @RequestParam(required = true, defaultValue = "") String uid,
                                                      @RequestParam(required = true, defaultValue = "") String channel,
                                                      @RequestParam(required = true, defaultValue = "1") int type) {
        return chatService.recordStatus(userId, token, resourceId, sid, type);
    }


    /**
     * 普通用户根据resourceId和sid停止录播
     *
     * @param userId     用户id
     * @param token      用户token
     * @param resourceId 通过请求获取的 resource ID。
     * @param sid        通过 start 请求获取的录制 ID。
     * @param uid        录制uid
     * @param channel    频道号
     * @param ids        聊天交流对象id(登录用户传)
     * @param type       1普通录制 2应急求救
     */
    @PostMapping("recordStop")
    public ApiResult<RecordStopResult> recordStop(@RequestParam(required = true, defaultValue = "0") Integer userId,
                                                  @RequestParam(required = true, defaultValue = "") String token,
                                                  @RequestParam(required = true, defaultValue = "") String resourceId,
                                                  @RequestParam(required = true, defaultValue = "") String sid,
                                                  @RequestParam(required = true, defaultValue = "") String uid,
                                                  @RequestParam(required = true, defaultValue = "") String channel,
                                                  @RequestParam(required = false, defaultValue = "0") Integer ids,
                                                  @RequestParam(required = true, defaultValue = "1") int type) {
        return chatService.recordStop(userId, token, resourceId, sid, uid, channel, ids, type);
    }


    /**
     * 普通用户充值调起支付
     *
     * @param userId  用户id
     * @param token   用户token
     * @param type    1单次支付 2按月支付 3按年支付
     * @param payType 1电脑支付 2app 3积分
     */
    @PostMapping("payOrder")
    public ApiResult<String> payOrder(@RequestParam(required = true, defaultValue = "0") Integer userId,
                                      @RequestParam(required = true, defaultValue = "") String token,
                                      @RequestParam(required = true, defaultValue = "") Integer type,
                                      @RequestParam(required = true, defaultValue = "1") Integer payType) {
        return chatService.payOrder(userId, token, type, payType);
    }

    /**
     * 用户根据用户标识符查询被录制状态
     *
     * @param ids id或重命名名称
     */
    @PostMapping("recordingStatus")
    public ApiResult<RecordingResult> recordingStatus(@RequestParam(required = true, defaultValue = "") String ids) {
        return chatService.recordingStatus(ids);
    }


    /**
     * 结束聊天
     *
     * @param userId 登录用户id
     * @param uuid   未登录uuid
     * @param ids    匹配id
     */
    @PostMapping("endChat")
    public ApiResult<Base> endChat(@RequestParam(required = true, defaultValue = "0") Integer userId,
                                   @RequestParam(required = true, defaultValue = "") String uuid,
                                   @RequestParam(required = true, defaultValue = "0") Integer ids,
                                   HttpServletRequest request) {
        return chatService.endChat(userId, uuid, ids, request);
    }

    /**
     * 上传截图
     *
     * @param userId 登录用户id
     * @param uuid   未登录uuid
     * @param mateId 匹配id
     * @param file   文件
     */
    @PostMapping("uploadScreen")
    public ApiResult<String> uploadScreen(@RequestParam(required = true, defaultValue = "0") Integer userId,
                                          @RequestParam(required = true, defaultValue = "") String uuid,
                                          @RequestParam(required = true, defaultValue = "0") Integer mateId,
                                          MultipartFile file,
                                          HttpServletRequest request) {
        return adminService.uploadScreen(userId, uuid, mateId, file, request);
    }

    /**
     * 获取头像
     *
     * @param userId 已登录用户id
     */
    @PostMapping("getIcon")
    public ApiResult<String> getIcon(@RequestParam(required = true, defaultValue = "0") Integer userId,
                                     MultipartFile file,
                                     HttpServletRequest request) {
        return adminService.getIcon(userId, file, request);
    }

    /**
     * 获取用户截图
     *
     * @param userId 登录用户id
     * @param uuid   未登录uuid
     * @param mateId 匹配id
     */
    @PostMapping("getScreenshot")
    public ApiResult<String> getScreenshot(@RequestParam(required = true, defaultValue = "0") Integer userId,
                                           @RequestParam(required = true, defaultValue = "") String uuid,
                                           @RequestParam(required = true, defaultValue = "0") Integer mateId,
                                           HttpServletRequest request) {
        return adminService.getScreenshot(userId, uuid, mateId, request);
    }

    /**
     * 拉黑/取消拉黑 普通用户/管理员
     *
     * @param userId    用户id
     * @param token     用户token
     * @param ids       普通用户/管理员id
     * @param blackList 是否黑名单 1是 2否
     */
    @PostMapping("blackAdmin")
    public ApiResult<Base> blackAdmin(@RequestParam(required = true, defaultValue = "0") Integer userId,
                                      @RequestParam(required = true, defaultValue = "") String token,
                                      @RequestParam(required = true, defaultValue = "0") Integer ids,
                                      @RequestParam(required = true, defaultValue = "") Integer blackList) {
        return adminService.blackAdmin(userId, token, ids, blackList);
    }


    /**
     * 文章列表
     *
     * @param title 文章标题
     * @param page  页数
     * @param limit 每页数量
     */
    @PostMapping("listContent")
    public ListResult<List<ContentResult>> listContent(@RequestParam(required = true, defaultValue = "") String title,
                                                       @RequestParam(required = true, defaultValue = "1") Integer page,
                                                       @RequestParam(required = true, defaultValue = "10") Integer limit) {
        return contentService.listContent(title, page, limit);
    }


    /**
     * 添加文章
     *
     * @param userId  用户id
     * @param token   用户token
     * @param title   文章标题
     * @param content 文章内容
     */
    @PostMapping("addContent")
    public ApiResult<Base> addContent(@RequestParam(required = true, defaultValue = "0") Integer userId,
                                      @RequestParam(required = true, defaultValue = "") String token,
                                      @RequestParam(required = true, defaultValue = "") String title,
                                      @RequestParam(required = true, defaultValue = "") String content) {
        return contentService.addContent(userId, token, title, content);
    }


    /**
     * 编辑文章
     *
     * @param userId  用户id
     * @param token   用户token
     * @param ids     文章id
     * @param title   文章标题
     * @param content 文章内容
     */
    @PostMapping("editContent")
    public ApiResult<Base> editContent(@RequestParam(required = true, defaultValue = "0") Integer userId,
                                       @RequestParam(required = true, defaultValue = "") String token,
                                       @RequestParam(required = true, defaultValue = "0") Integer ids,
                                       @RequestParam(required = true, defaultValue = "") String title,
                                       @RequestParam(required = true, defaultValue = "") String content) {
        return contentService.editContent(userId, token, ids, title, content);
    }


    /**
     * 删除文章
     *
     * @param userId 用户id
     * @param token  用户token
     * @param ids    文章id
     */
    @PostMapping("deleteContent")
    public ApiResult<Base> deleteContent(@RequestParam(required = true, defaultValue = "0") Integer userId,
                                         @RequestParam(required = true, defaultValue = "") String token,
                                         @RequestParam(required = true, defaultValue = "ids[]") Integer[] ids) {
        return contentService.deleteContent(userId, token, ids);
    }

    /**
     * 在线心跳更新
     *
     * @param userId 登录用户id
     * @param uuid   未登录uuid
     */
    @PostMapping("updateOnline")
    public ApiResult<Integer> updateOnline(@RequestParam(required = true, defaultValue = "0") Integer userId,
                                           @RequestParam(required = true, defaultValue = "") String uuid,
                                           HttpServletRequest request) {
        return chatService.updateOnline(userId, uuid, request);
    }
    /**
     * 在线心跳更新
     *
     * @param userId 登录用户id
     * @param uuid   未登录uuid
     */
    @PostMapping("updateOnlineNew")
    public ApiResult<OnlineResult> updateOnlineNew(@RequestParam(required = true, defaultValue = "0") Integer userId,
                                           @RequestParam(required = true, defaultValue = "") String uuid) {
        return chatService.updateOnlineNew(userId, uuid);
    }

    /**
     * 获取版本号（相同 true 不同 false）
     *
     * @param version 版本号
     */
    @PostMapping("getVersion")
    public ApiResult<Boolean> getVersion(@RequestParam(defaultValue = "", required = true) String version) {
        return adminService.getVersion(version);
    }

    /**
     * 获取版本号
     */
    @PostMapping("getNewVersion")
    public ApiResult<String> getNewVersion() {
        return adminService.getNewVersion();
    }

    /**
     * 紧急呼叫开始录制
     *
     * @param uuid    未登录uuid
     * @param channel 频道号
     * @param ids     对方id或对方重命名名称
     */
    @PostMapping("emergencyRecord")
    public ApiResult<RecordResult> emergencyRecord(@RequestParam(required = true, defaultValue = "") String uuid,
                                                   @RequestParam(required = true, defaultValue = "") String channel,
                                                   @RequestParam(required = true, defaultValue = "") String ids) {
        return chatService.emergencyRecord(uuid, channel, ids);
    }


    @PostMapping("notify")
    public String alipayNotify(HttpServletRequest request, String out_trade_no, String trade_no, String trade_status, String subject) throws AlipayApiException {
        Map<String, String> params = new HashMap<>();
        Map<String, String[]> parameterMap = request.getParameterMap();
        for (Map.Entry<String, String[]> entry : parameterMap.entrySet()) {
            String[] values = entry.getValue();
            String valueStr = "";
            for (int i = 0; i < values.length; i++) {
                valueStr = (i == values.length - 1) ? valueStr + values[i] : valueStr + values[i] + ",";
            }
            params.put(entry.getKey(), valueStr);
        }
        // 要删除该字段,不然校验签名会失败
        params.remove("sign_type");

        logger.info("notify params: {}" + JSONObject.toJSON(params));
        // 验证签名
        boolean signVerified = AlipaySignature.rsaCheckV1(params, AlipayConfig.alipay_public_key, AlipayConfig.charset, AlipayConfig.sign_type);
        logger.info("notify signVerified: {}" + signVerified);
        if (signVerified) {
            //处理你的业务逻辑，更新订单状态等
            return orderService.dealSuccessPay(out_trade_no, subject, trade_status);
        } else {
            logger.info("验证失败,不去更新状态");
            return ("fail");
        }
    }

    @PostMapping("notify1")
    public String alipayNotify1(HttpServletRequest request, String out_trade_no, String trade_no, String trade_status, String subject) throws AlipayApiException {
        Map<String, String> params = new HashMap<String, String>();
        Map requestParams = request.getParameterMap();
        for (Iterator iter = requestParams.keySet().iterator(); iter.hasNext(); ) {
            String name = (String) iter.next();
            String[] values = (String[]) requestParams.get(name);
            String valueStr = "";
            for (int i = 0; i < values.length; i++) {
                valueStr = (i == values.length - 1) ? valueStr + values[i] : valueStr + values[i] + ",";
            }
            //乱码解决，这段代码在出现乱码时使用。
            //valueStr = new String(valueStr.getBytes("ISO-8859-1"), "utf-8");
            params.put(name, valueStr);
        }

        logger.info("notify params: {}" + JSONObject.toJSON(params));
        // 验证签名
        boolean signVerified = AlipaySignature.rsaCheckV1(params, AlipayConfig.app_alipay_public_key, AlipayConfig.charset, AlipayConfig.sign_type);
        logger.info("notify signVerified: {}" + signVerified);
        if (signVerified) {
            //处理你的业务逻辑，更新订单状态等
            return orderService.dealSuccessPay(out_trade_no, subject, trade_status);
        } else {
            logger.info("验证失败,不去更新状态");
            return ("fail");
        }
    }

    /**
     * 支付宝积分提现
     *
     * @param userId  用户id
     * @param token   用户token
     * @param point  提现积分
     * @param zfbName  收款方姓名
     * @param zfbAccount  支付宝账号
     */
    @PostMapping("withdrawal")
    public ApiResult<Integer> withdrawal(@RequestParam(required = true, defaultValue = "0") Integer userId,
                                         @RequestParam(required = true, defaultValue = "") String token,
                                         @RequestParam(required = true, defaultValue = "") Integer point,
                                         @RequestParam(required = true, defaultValue = "") String zfbName,
                                         @RequestParam(required = true, defaultValue = "") String zfbAccount) {
        return adminService.withdrawal(userId, token, point, zfbName, zfbAccount);
    }

    /**
     * 查询积分提现列表
     *
     * @param userId  用户id
     * @param token   用户token
     */
    @PostMapping("listWithdrawal")
    public ListResult<List<PointWithdrawal>> listWithdrawal(@RequestParam(required = true, defaultValue = "0") Integer userId,
                                                            @RequestParam(required = true, defaultValue = "") String token,
                                                            @RequestParam(required = true, defaultValue = "1") Integer page,
                                                            @RequestParam(required = true, defaultValue = "10") Integer limit,
                                                            @RequestParam(required = false, defaultValue = "") Integer status,
                                                            @RequestParam(required = false, defaultValue = "") String phone) {
        return pointWithdrawalService.withdrawalList(userId, token, page, limit, status, phone);
    }

    /**
     * 更新积分提现状态为已打款
     *
     * @param userId  用户id
     * @param token   用户token
     * @param ids     提现记录ID
     */
    @PostMapping("auditWithdrawal")
    public ApiResult<Base> auditWithdrawal(@RequestParam(required = true, defaultValue = "0") Integer userId,
                                           @RequestParam(required = true, defaultValue = "") String token,
                                           @RequestParam(required = true, defaultValue = "0") Integer ids) {
        return pointWithdrawalService.auditWithdrawal(userId, token, ids);
    }

    /**
     * 添加视频分类
     *
     */
    @PostMapping("addVideoType")
    public ApiResult<Base> addVideoType(@RequestParam(required = true, defaultValue = "0") Integer userId,
                                      @RequestParam(required = true, defaultValue = "") String token,
                                      @RequestParam(required = true, defaultValue = "") String name,
                                      @RequestParam(required = true, defaultValue = "0") int sort) {
        return videoTypeService.addVideoType(userId, token, name, sort);
    }

    /**
     * 修改/删除视频分类
     *
     */
    @PostMapping("updateVideoType")
    public ApiResult<Base> updateVideoType(@RequestParam(required = true, defaultValue = "0") Integer userId,
                                        @RequestParam(required = true, defaultValue = "") String token,
                                        @RequestParam(required = true, defaultValue = "") long id,
                                           @RequestParam(required = true, defaultValue = "0") int isDelete,
                                        @RequestParam(required = true, defaultValue = "") String name,
                                        @RequestParam(required = true, defaultValue = "0") int sort) {
        return videoTypeService.updateVideoType(userId, token, id, isDelete, name, sort);
    }

    /**
     * 获取视频分类
     *
     */
    @GetMapping("getVideoType")
    public ApiResult<List<VideoType>> getVideoType(@RequestParam(required = false, defaultValue = "") String name) {

        List<VideoType> list = videoTypeService.list(new LambdaQueryWrapper<VideoType>()
                .eq(VideoType::getIsDelete, 0)
                .like(StringUtils.isNotBlank(name), VideoType::getName, name)
                .orderByAsc(VideoType::getSort));
        return new ApiResult<>(1, "查询成功!", list);
    }

    /**
     * 修改视频标题，选择分类发布展示
     *
     * @param userId  用户id
     * @param token   用户token
     * @param id     视频id
     * @param name    视频名称
     * @param videoType    视频类型
     * @param type    是否发布 1:是 0:否
     */
    @PostMapping("publishVideo")
    public ApiResult<Base> publishVideo(@RequestParam(required = true, defaultValue = "0") Integer userId,
                                        @RequestParam(required = true, defaultValue = "") String token,
                                        @RequestParam(required = true, defaultValue = "0") Integer id,
                                        @RequestParam(required = true, defaultValue = "") String name,
                                        @RequestParam(required = false, defaultValue = "") String cover,
                                        @RequestParam(required = true, defaultValue = "1") Integer videoType,
                                        @RequestParam(required = true, defaultValue = "0") int type) {
        return videoService.publishVideo(userId, token, id, name, cover, videoType, type);
    }

    /**
     * 后台审核视频/违规下架
     *
     * @param userId  用户id
     * @param token   用户token
     * @param id     视频id
     * @param type    是否通过 1:是 0:否 2:违规下架 3 置为A区 4 取消置为A区
     */
    @PostMapping("auditVideo")
    public ApiResult<Base> auditVideo(@RequestParam(required = true, defaultValue = "0") Integer userId,
                                        @RequestParam(required = true, defaultValue = "") String token,
                                        @RequestParam(required = true, defaultValue = "0") long id,
                                        @RequestParam(required = true, defaultValue = "0") int type) {
        return videoService.auditVideo(userId, token, id, type);
    }

    /**
     * B区/C区视频列表
     *
     * @param name 视频标题
     * @param type B区、C区查询
     * @param videoType 视频类型
     * @param page  页数
     * @param limit 每页数量
     */
    @GetMapping("videoList")
    public ListResult<List<VideoResult>> listVideoBytype(@RequestParam(required = true, defaultValue = "1") Integer page,
                                                         @RequestParam(required = true, defaultValue = "10") Integer limit,
                                                         @RequestParam(required = true, defaultValue = "C") String type,
                                                         @RequestParam(required = false, defaultValue = "") String name,
                                                         @RequestParam(required = false, defaultValue = "") String username,
                                                         @RequestParam(required = false, defaultValue = "") String startTime,
                                                         @RequestParam(required = false, defaultValue = "") String endTime,
                                                         @RequestParam(required = false, defaultValue = "") Integer status,
                                                         @RequestParam(required = false) Long videoType,
                                                         @RequestParam(required = false, defaultValue = "") Integer isTop) {
        return videoService.listVideoBytype(type, name, videoType, username, startTime, endTime, status,isTop, page, limit);
    }

    /**
     * 点击/点赞接口
     * @param userId  用户id
     * @param token   用户token
     * @param id     视频id
     * @param type    1：点击 2：点赞 3：取消点赞
     */
    @PostMapping("clickOrLike")
    public ApiResult<Map<String, Object>> clickOrLike(@RequestParam(required = false, defaultValue = "0") Integer userId,
                                       @RequestParam(required = false, defaultValue = "") String token,
                                       @RequestParam(required = true, defaultValue = "0") Integer id,
                                       @RequestParam(required = true, defaultValue = "0") int type) {

        return videoService.clickOrLike(userId, token, id, type);
    }

    /**
     * 添加banner
     * @param userId  用户id
     * @param token   用户token
     */
    @PostMapping("addBanner")
    public ApiResult<Base> addBanner(@RequestParam(required = true, defaultValue = "0") Integer userId,
                                       @RequestParam(required = true, defaultValue = "") String token,
                                       @RequestParam(required = true, defaultValue = "0") String title,
                                       @RequestParam(required = true) String img,
                                     @RequestParam(required = false) String linkUrl,
                                     @RequestParam(required = false) Integer videoId,
                                       @RequestParam(required = true, defaultValue = "0") int type,
                                     @RequestParam(required = true, defaultValue = "0") int status) {

        return bannerService.addBanner(userId, token, title, img, linkUrl, videoId, type, status);
    }

    /**
     * 修改banner
     * @param userId  用户id
     * @param token   用户token
     */
    @PostMapping("updateBanner")
    public ApiResult<Base> updateBanner(@RequestParam(required = true, defaultValue = "0") Integer userId,
                                     @RequestParam(required = true, defaultValue = "") String token,
                                     @RequestParam(required = true, defaultValue = "0") String title,
                                     @RequestParam(required = true) long id,
                                     @RequestParam(required = true) String img,
                                     @RequestParam(required = false) String linkUrl,
                                     @RequestParam(required = false) Integer videoId,
                                     @RequestParam(required = true, defaultValue = "0") int type,
                                     @RequestParam(required = true, defaultValue = "0") int status) {

        return bannerService.updateBanner(userId, token, id, title, img, linkUrl, videoId, type, status);
    }

    @PostMapping("deleteBanner")
    public ApiResult<Base> deleteBanner(@RequestParam(required = true, defaultValue = "0") Integer userId,
                                            @RequestParam(required = true, defaultValue = "") String token,
                                            @RequestParam(required = true, defaultValue = "") Integer id) {
        return bannerService.deleteBanner(userId, token, id);
    }

    @GetMapping("bannerList")
    public ListResult<List<Banner>> bannerList(@RequestParam(required = true, defaultValue = "1") Integer page,
                                              @RequestParam(required = true, defaultValue = "10") Integer limit,
                                              @RequestParam(required = false, defaultValue = "") String title,
                                              @RequestParam(required = false, defaultValue = "") Integer type) {

        return bannerService.getBannerList(page, limit, title, type);
    }

    @GetMapping("bannerForA")
    public ApiResult<List<Banner>> bannerForA() {
        return bannerService.bannerForA();
    }
}
